/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.ktmall.utils;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 请求相关工具类
 * @author 科泰集团教研团队
 * @createTime 2024-4-17 20:49:54
 * @lastModify 2024-4-17 20:49:54
 * @version 1.0.0
 */
public class RequestUtil {

    /**
     * 转换长整数参数
     * @param request
     * @param name
     * @param defaultVal
     * @return
     */
    public static Long parseLong(HttpServletRequest request, String name, Long defaultVal){
        String value = request.getParameter(name);
        if(value != null){
            try {
                return Long.parseLong(value);
            }catch (Exception e){}
        }
        return defaultVal;
    }

    /**
     * 转换整数参数
     * @param request
     * @param name
     * @param defaultVal
     * @return
     */
    public static Integer parseInt(HttpServletRequest request, String name, Integer defaultVal){
        String value = request.getParameter(name);
        if(value != null){
            try {
                return Integer.parseInt(value);
            }catch (Exception e){}
        }
        return defaultVal;
    }

    public static Date parseDate(HttpServletRequest request, String name, Date defaultVal, String...dateFormats){
        String value = request.getParameter(name);
        SimpleDateFormat sdf = new SimpleDateFormat();
        if(value != null){
            try {
                return DateUtils.parseDate(value, dateFormats);
            }catch (Exception e){}
        }
        return defaultVal;
    }

    public static Integer[] parseIntValues(HttpServletRequest request, String name, Integer defaultVal){
        String[] stringValues = request.getParameterValues(name);
        Integer[] values = new Integer[stringValues.length];
        if(stringValues != null){
            for (int i = 0; i < stringValues.length; i++) {
                String parseVal = stringValues[i];
                try {
                    values[i] = Integer.parseInt(parseVal);
                }catch (Exception e){}
            }
        }
        return values;
    }

    public static Long[] parseLongValues(HttpServletRequest request, String name, Long defaultVal){
        String[] stringValues = request.getParameterValues(name);
        Long[] values = new Long[stringValues.length];
        if(stringValues != null){
            for (int i = 0; i < stringValues.length; i++) {
                String parseVal = stringValues[i];
                try {
                    values[i] = Long.parseLong(parseVal);
                }catch (Exception e){}
            }
        }
        return values;
    }

    public static <T> List<T> readImportFile(HttpServletRequest request, String name, Class<T> clz, String...notEmptyFields)
        throws Exception{
        // 读取文件
        Part importFile = request.getPart(name);
        InputStream is = importFile.getInputStream();
        // 使用easyPoi导入
        ImportParams params = new ImportParams();

        List<T> list = ExcelImportUtil.importExcel(is, clz, params);
        // 删除空对象
        if(notEmptyFields != null && notEmptyFields.length != 0){
            list.removeIf(item ->{
                // 循环每个字段，要求字段不能为null或empty
                for (int i = 0; i < notEmptyFields.length; i++) {
                    String notEmptyField = notEmptyFields[i];
                    try {
                        PropertyDescriptor pd = new PropertyDescriptor(notEmptyField, clz);
                        Object value = pd.getReadMethod().invoke(item);
                        if(value == null){
                            return true;
                        }
                        if(value instanceof String){
                            String val = (String)value;
                            return StringUtils.isEmpty(val);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return false;
            });
        }
        return list;
    }

}
